"use strict";
var MySnake;
(function (MySnake) {
    //蛇
    var Snake = /** @class */ (function () {
        //构造函数 初始化
        function Snake(_speed, _rowCount, _columnCount) {
            if (_speed === void 0) { _speed = 300; }
            if (_rowCount === void 0) { _rowCount = 50; }
            if (_columnCount === void 0) { _columnCount = 50; }
            var _this = this;
            this.Bodies = new Array(); //蛇身
            this.IsPaused = true; //是否已经暂停
            this._Timer = 1000; //计数器
            this.Speed = _speed;
            this.RowCount = _rowCount;
            this.ColumnCount = _columnCount;
            this.Audio = document.getElementById("bgMusic");
            this.Container = document.getElementById("tabContaniner");
            this.Direction = Math.floor(Math.random() * 4); //0-4 随机整数
            MySnake.Music.SoundControl(this.Audio, 0.2); //初始化音量（0.0-1.0）
            var x, y; //坐标
            //画表格
            for (var i_1 = 0; i_1 < this.RowCount; i_1++) {
                var _Row = this.Container.insertRow(-1);
                for (var j = 0; j < this.ColumnCount; j++) {
                    _Row.insertCell(-1);
                }
            }
            //随机产生食物
            for (var i = 0; i < 50; i++) {
                this.GenerateFood();
            }
            //产生蛇头
            while (true) {
                debugger;
                x = Math.floor(Math.random() * this.ColumnCount);
                y = Math.floor(Math.random() * this.RowCount);
                if (!MySnake.Draw.IsCellFilled(this.Container, x, y)) {
                    this.Container.rows[y].cells[x].style.backgroundColor = MySnake.Draw.GetRandomColor();
                    var _body = new MySnake.SnakeBody();
                    _body.X = x;
                    _body.Y = y;
                    for (var i_2 = 0; i_2 < 3; i_2++) {
                        this.Bodies.push(_body);
                    }
                    break;
                }
            }
            //事件响应
            document.onkeydown = function (e) {
                //监听键盘事件产生食物
                //this._Timer = setInterval(() => this.GenerateFood(), 10000);
                switch (e.keyCode | e.which | e.charCode) {
                    //Enter 回车键
                    case 13:
                        if (_this.IsPaused) {
                            _this.Run();
                            MySnake.Music.GoGimes(_this.Audio, MySnake.PalyState.Play);
                            _this.GimesMsg("游戏进行中!");
                        }
                        else {
                            _this.Pause(); //如果没有暂停，则停止移动
                            MySnake.Music.GoGimes(_this.Audio, MySnake.PalyState.Pause);
                            _this.GimesMsg("游戏暂停!");
                        }
                        break;
                    //左箭头
                    case 37:
                        //阻止蛇倒退走
                        if (_this.Direction == MySnake.MoveDirection.Right) {
                            break;
                        }
                        _this.Direction = MySnake.MoveDirection.Left;
                        break;
                    //上箭头
                    case 38:
                        //阻止蛇倒退走
                        if (_this.Direction == MySnake.MoveDirection.Down) {
                            break;
                        }
                        _this.Direction = MySnake.MoveDirection.Up;
                        break;
                    //右箭头
                    case 39:
                        //阻止蛇倒退走
                        if (_this.Direction == MySnake.MoveDirection.Left) {
                            break;
                        }
                        _this.Direction = MySnake.MoveDirection.Right;
                        break;
                    //下箭头
                    case 40:
                        //阻止蛇倒退走
                        if (_this.Direction == MySnake.MoveDirection.Up) {
                            break;
                        }
                        _this.Direction = MySnake.MoveDirection.Down;
                        break;
                }
            };
        }
        //暂停
        Snake.prototype.Pause = function () {
            clearInterval(this._Timer);
            if (!this.IsPaused) {
                this.IsPaused = true;
            }
            MySnake.Draw.Paint(this.Bodies, this.Container);
        };
        //蛇身运行到下一节点
        Snake.prototype.MoveNextStep = function () {
            //debugger;
            //游戏结束: 【到达边界】或者【碰到蛇身】
            if (MySnake.Draw.CheckNextStep(this.Bodies, this.Direction, this.ColumnCount, this.RowCount, this.Container) == -1) {
                MySnake.Music.GoGimes(this.Audio, MySnake.PalyState.Stop);
                //alert("游戏结束");
                this.GimesMsg("游戏结束!");
                this.Pause(); //游戏暂停
                return;
            }
            //遇到食物
            if (MySnake.Draw.CheckNextStep(this.Bodies, this.Direction, this.ColumnCount, this.RowCount, this.Container) == 1) {
                this.GimesMsg("吃到食物了!");
                //产生食物
                this.GenerateFood();
                var newBody_1 = MySnake.Draw.GetNextPosition(this.Bodies, this.Direction);
                this.Bodies.unshift(newBody_1); //长身体
                return;
            }
            var newBody = MySnake.Draw.GetNextPosition(this.Bodies, this.Direction);
            this.Bodies.unshift(newBody);
            this.Bodies.pop(); //没吃到食物，就移除最后的点元素
        };
        //蛇身运动
        Snake.prototype.Run = function () {
            if (this.IsPaused) {
                this.IsPaused = false;
            }
            var _Snake = this;
            this._Timer = setInterval(function () {
                MySnake.Draw.Erase(_Snake.Bodies, _Snake.Container);
                _Snake.MoveNextStep();
                MySnake.Draw.Paint(_Snake.Bodies, _Snake.Container);
            }, this.Speed);
        };
        //随机产生食物
        Snake.prototype.GenerateFood = function () {
            var x = Math.floor(Math.random() * this.ColumnCount);
            var y = Math.floor(Math.random() * this.RowCount);
            if (!MySnake.Draw.IsCellFilled(this.Container, x, y)) {
                this.Container.rows[y].cells[x].style.backgroundColor = MySnake.Draw.GetRandomColor();
            }
        };
        //游戏提示
        Snake.prototype.GimesMsg = function (msg) {
            console.log(msg);
        };
        return Snake;
    }());
    MySnake.Snake = Snake;
    //运行调用
    window.onload = function () {
        //debugger;
        //let _Snake = new Snake();
        var _Snake = new Snake(500, 50, 50);
    };
})(MySnake || (MySnake = {}));
